var app = getApp()

Page({
  data:{
    activity_id : 0,
    task : {},
    answer : "",
    hideFinishDialog : true,
    card_type : ""
  },
  onLoad:function(options){
    var that = this;
    var id = options.id;
    this.setData({
        activity_id : options.id
    });
    app.request("task/activity/process/"+id, "GET", null, function(res){
      that.setData({
          task : res.result,
          tasktype : JSON.parse(res.result.type).name,
          card_type: JSON.parse(res.result.type).type
      });
    }, function(res){
        if(res.code == 1){
          //当前无可执行任务，获取下一个任务
          that.getNextTask();
        }else if(res.code == 90){
          app.showAlert("提示", res.msg, function(){
              wx.navigateBack(1);
          });
        }
    });
    
  },

  getNextTask : function(){
    var that = this;
    app.showLoading("正在获取任务");
    var id = this.data.activity_id;
    app.request("task/accept/activity/process/"+id, "GET", null, function(res){
      app.closeLoading();
      wx.redirectTo({
        url: 'task?id='+id+'&join=false'
      })
    }, function(res){
      app.closeLoading();
      if(res.code == 1){
        //完成所有任务
        that.showFinishDialog();
      }else {
        app.showAlert("提示", res.msg, function(){
            wx.navigateBack(1);
        });
      }
    });
  },

  finish_task : function(){
    var card_type = this.data.card_type;
    if(card_type == "QA"){
      this.submit(this.data.answer);
    }else if(card_type == "UPLOAD"){
      this.uploadImg();
    }else if(card_type == "SCAN"){
      this.scan();
    }
    
  },

  answer_input : function(event){
    this.setData({
      answer : event.detail.value
    });
  },

  submit : function(value){
    var that = this;
    app.showLoading("正在提交答案");
    var data = { "task": this.data.task.id, "type": this.data.card_type,"value":value};
    app.request("task/complate/activity/process/" + this.data.activity_id, "POST", data, function(res){
      app.closeLoading();
        wx.showModal({
          title: res.msg,
          content: '是否继续下一项?',
          success: function(res) {
            if (res.confirm) {
              that.getNextTask();
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    }, function(data){
      app.closeLoading();
      app.showAlert("提示", data.msg);
    });
  },

  uploadImg : function(){
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        app.showLoading("正在上传图片");
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0]
        app.uploadFile(tempFilePaths, function(res){
          app.closeLoading();
          that.submit(res.data.result.id);
        });
      }
    })

  },

  scan : function(){
    var that = this;
    wx.scanCode({
      success: function(res){
        that.submit(res.result);
      }
    })
  },

  showFinishDialog : function(){
      this.setData({
          hideFinishDialog : false
      })
  },

  finishConfirm:function() {
      this.setData({
          hideFinishDialog: true
      })
      wx.navigateBack(1);
      var url = "http://www.86xcz.cn/img/0x1.png";
      wx.previewImage({
        current: url, // 当前显示图片的http链接
        urls: [url] // 需要预览的图片http链接列表
      })
  },

  finishCancel:function() {
      this.setData({
          hideFinishDialog: true
      })
      wx.navigateBack(1);
  },

})